import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import Home from "../views/Home.vue";
import Login from "../views/login/Login.vue";
import Register from "../views/login/Register.vue"
import SingleDiagnosis from "../components/SingleDiagnosis.vue"
import BatchDiagnostics from "../components/BatchDiagnosis.vue"
import IntelligentMedical from "../components/IntelligentMedical.vue"
import DataAnalysis from "../components/DataAnalysis.vue"
import DiseaseScience from "../components/DiseaseScience.vue"
import OnlineConsultation from "../components/OnlineConsultation.vue"
import QuestionDetail from "../components/QuestionDetail.vue";
import IdentityAuthentication from "../components/IdentityAuthentication.vue";
import UserManagement from "../components/UserManagement.vue";
import ModelManagement from "../components/ModelManagement.vue";
import ScienceVideo from "../components/ScienceVideo.vue";
import DrugInquiry from "../components/DrugInquiry.vue";
import VideoDetail from "../components/VideoDetail.vue";
import DrugManagement from "../components/DrugManagement.vue";
import InternalPage from "../components/InternalPage.vue";
import LiveQA from "../components/LiveQA.vue";
import LiveQADetail from "../components/LiveQADetail.vue";

import {checkToken} from "@/api/login";

Vue.use(VueRouter);

const router = new VueRouter({

    routes: [
        {
            path: "/",
            redirect: "login",
        },
        {
            path: "/login",
            name: "Login",
            component: Login,
        },
        {
            path: "/register",
            name: "Register",
            component: Register,
        },
        {
            path: "/home",
            name: "Home",
            component: Home,
            children: [
                {
                    path: 'singleDiagnosis',
                    component: SingleDiagnosis,
                },
                {
                    path: 'batchDiagnosis',
                    component: BatchDiagnostics,
                },
                {
                    path: 'intelligentMedical',
                    component: IntelligentMedical
                },
                {
                    path: 'diseaseScience',
                    component: DiseaseScience
                },
                {
                    path: 'dataAnalysis',
                    component: DataAnalysis
                },
                {
                    path: 'onlineConsultation',
                    component: OnlineConsultation
                },
                {
                    path: 'questionDetail',
                    component: QuestionDetail
                },
                {
                    path: 'identityAuthentication',
                    component: IdentityAuthentication
                },
                {
                    path: 'userManagement',
                    component: UserManagement
                },
                {
                    path: 'modelManagement',
                    component: ModelManagement
                },
                {
                    path: 'scienceVideo',
                    component: ScienceVideo
                },
                {
                    path: 'drugInquiry',
                    component: DrugInquiry
                },
                {
                    path: 'videoDetail',
                    component: VideoDetail
                },
                {
                    path: 'drugManagement',
                    component: DrugManagement
                },
                {
                    path: 'liveQA',
                    component: LiveQA
                },
                {
                    path: 'liveQADetail',
                    component: LiveQADetail
                },
            ]
        },
        // 这个页面是内嵌在数据大屏中的页面 通过系统的menu是访问不到的
        {
            path: "/internalPage",
            name: "InternalPage",
            component: InternalPage,
        },
    ],
});

//当路由开始跳转时
router.beforeEach((to, from, next) => {
    // 开启进度条
    NProgress.start();
    // 获取token
    const token = sessionStorage.getItem('token')
    //2个不用token的页面请求
    if (to.path === '/login' || to.path === '/register') {
        return next()
    }

    // 数据大屏免登录
    if (to.path === '/internalPage') {
        return next()
    }
    // 如果token存在 先检查下
    if (token) {
        checkToken().then(res => {
            if (res.code !== 200) {
                // token无效 移除token 返回登陆页
                sessionStorage.removeItem('token')
                return next('/login')
            } else {
                return next();
            }
        })
    } else {
        //没有token的情况 直接返回登录页
        return next('/login')
    }
});
//当路由跳转结束后
router.afterEach(() => {
    // 关闭进度条
    NProgress.done()
})

// 将路由实例导出
export default router;